package com.neuedu.javase.jdbc.dao;

import java.sql.*;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-11 8:43
 * 描述 :
 */
public abstract class BaseDao {

    private String url = "jdbc:mysql://localhost:3306/jdbc_hrb20?useSSL=false&useUnicode=true&characterEncoding=utf8";
    private String username = "root";
    private String password = "root"; // ?????确认下
    private String driverClass = "com.mysql.jdbc.Driver";


    /**
     * 加载驱动，创建连接
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public Connection getConnection() throws ClassNotFoundException, SQLException {

        Class.forName(driverClass);
        Connection con = DriverManager.getConnection(url, username, password);


        return con;
    }


    /**
     * 关闭资源
     * @param con
     * @param stmt
     */
    public void close(Connection con, Statement stmt){
        close(con,stmt,null);
    }

    public void close(Connection con, Statement stmt , ResultSet rs){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con != null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }





}
